//
//  DYWReweetWeiBo.swift
//  sinaWeibo
//
//  Created by 董艳武 on 16/2/23.
//  Copyright © 2016年 董艳武. All rights reserved.
//

import UIKit
import SnapKit

class DYWReweetWeiBo: UIView {
    
    /// 设置约束属性，保存要更改的属性
    var changedConstraint:Constraint?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = HotPink
        setUI()

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    // MARK:-- 模型属性
    /// 绑定模型属性
    var model:DYWWeiboModel?{
        didSet{
            if let rewteetText = model?.text {
                reweetLabel.text = "转发\(model!.user?.screen_name ?? "")微博：" + rewteetText
//                reweetLabel.text = rewteetText
            }
            imgsView.imgUrls = model?.imgUrls
            // 给模型属性赋值时，写在旧的约束，更新约束
            self.changedConstraint?.uninstall()
            // 根据数据调整约束关系
            if let urls = model?.imgUrls where urls.count != 0  {
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.changedConstraint = make.bottom.equalTo(imgsView.snp_bottom).offset(commonMargin).constraint
                })
                imgsView.hidden = false
                
            } else {
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.changedConstraint = make.bottom.equalTo(reweetLabel.snp_bottom).offset(commonMargin).constraint
                })
                imgsView.hidden = true
            }
            
        }
    }
    
    //MARK:-- 懒加载控件
    /// 懒加载转发微博的正文
    lazy var reweetLabel:UILabel = UILabel(fontSize: 14, alignment: .Left, contentColor: UIColor.darkGrayColor(), isSizeToFit: true, lineCount: 0)
    
    /// 懒加载转发才微博的配图视图
    lazy var imgsView:DYWWeiBoImageView = DYWWeiBoImageView()
    
    private func setUI () {
        addSubview(reweetLabel)
        addSubview(imgsView)
        
        reweetLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(commonMargin)
            make.left.equalTo(self.snp_left).offset(commonMargin)
            make.right.equalTo(self.snp_right).offset(-commonMargin)
        }
        
        imgsView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(reweetLabel.snp_bottom).offset(commonMargin)
            make.left.equalTo(self.snp_left).offset(commonMargin)
        }
        
        self.snp_makeConstraints { (make) -> Void  in
          self.changedConstraint = make.bottom.equalTo(imgsView.snp_bottom).offset(commonMargin).constraint
        }
        
        self.layer.cornerRadius = 4
        self.layer.masksToBounds = true
    }
    
    
    

}
